package Practice6_Internet.P2_TCP;

import Utils.io.IOUtils;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * C/S传文件
 */
public class PracticeTest02 {
    @Test
    public void client() throws IOException {
        try (Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
             OutputStream os = socket.getOutputStream();
             FileInputStream fis = new FileInputStream(new File("./src/test/resources/img104.jpg"))) {
            IOUtils.inputStream2OutputStream(fis, os);
        }
    }

    @Test
    public void server() throws IOException {
        try (ServerSocket serverSocket = new ServerSocket(9090);
             Socket socket = serverSocket.accept();
             InputStream is = socket.getInputStream();
             FileOutputStream fos = new FileOutputStream("./src/test/resources/img104_New.jpg")) {
            IOUtils.inputStream2OutputStream(is, fos);
        }
    }

    @Test
    public void test01() {
//        var f = new File("./src/test/resources/img104.jpg");
        File f = new File("./src/test/resources/img104.jpg");
        System.out.println(f.isFile());
    }
}
